PATHMac OS 8 and 9 Developer Documentation > Human Interface Toolbox > Appearance Manager >

Programming With the Appearance Manager


Making a Custom Definition Function Theme-Compliant

The MyEditTextFrameControlDefProc function, shown in Listing 3-6 , draws a frame for an editable text field that is defined as a custom control definition function. The MyEditTextFrameControlDefProc function calls the MyIsAppearancePresent function, described in Becoming a Client of the Appearance Manager , to determine whether the Appearance Manager is present. Depending upon the presence of the Appearance Manager, MyEditTextFrameControlDefProc branches between two functions to draw the frame.

If the Appearance Manager is not present, MyEditTextFrameControlDefProc calls the non-theme-compliant function, MyClassicEditTextFrameControlDefProc . MyClassicEditTextFrameControlDefProc first obtains the control rectangle, then supplies the control rectangle and a color to QuickDraw to draw the frame. However, an editable text frame drawn in this manner maintains the same "fixed" look in any appearance, and it cannot adapt to theme switches.

If the Appearance Manager is present, however, MyEditTextFrameControlDefProc calls the MyAppearanceSavvyEditTextFrameControlDefProc function. MyAppearanceSavvyEditTextFrameControlDefProc then passes the appropriate Appearance Manager constant for the drawing state ( kThemeStateActive or kThemeStateInactive ) to the function DrawThemeEditTextFrame . DrawThemeEditTextFrame draws the frame appropriately for the activity state and the current theme. And, when a theme switch occurs, the frame automatically takes on a look consistent with the current theme.

Listing 3-6   Drawing a custom definition function that is theme-compliant

static pascal SInt32 MyClassicEditTextFrameControlDefProc (
                                                        SInt16 /* varCode */,
                                                        ControlHandle control,
                                                        ControlDefProcMessage message,
                                                        SInt32 /* param */)
{
    Rect contrlRect;

    switch (message)
    {
        case drawCntl :

            contrlRect = (**control).contrlRect;
            InsetRect (&contrlRect,-1,-1);
            // We're pre-Appearance Mgr here, so always draw in black...
            PenNormal ( );
            // unless the control part code value in contrlHilite indicates
            // that the control is inactive (or disabled); if so, draw in gray
            if ((**control).contrlHilite >= 254)
                PenPat (&(qd.gray));
            FrameRect (&contrlRect);
            break;

        default :

            // other cases omitted for simplicity
            break;
    }

    return 0;
}

static pascal SInt32 MyAppearanceSavvyEditTextFrameControlDefProc (
                                                        SInt16 /* varCode */,
                                                        ControlHandle control,
                                                        ControlDefProcMessage message,
                                                        SInt32 /* param */)
{
    Rect contrlRect;

    switch (message)
    {
        case drawCntl :

            contrlRect = (**control).contrlRect;
            DrawThemeEditTextFrame (&contrlRect,
                ((**control).contrlHilite < 254) ?
                    kThemeStateActive : kThemeStateInactive);
            break;

        default :

            // other cases omitted for simplicity
            break;
    }

    return 0;
}

static pascal SInt32 MyEditTextFrameControlDefProc (
                                                    SInt16 varCode,
                                                    ControlHandle control,
                                                    ControlDefProcMessage message,
                                                    SInt32 param)
{
    OSStatus err = noErr;

    Boolean haveAppearance;

    if (!(err = MyIsAppearancePresent (&haveAppearance)))
    {
        if (haveAppearance)
            err = MyAppearanceSavvyEditTextFrameControlDefProc (
                                                    varCode,
                                                    control,
                                                    message,
                                                    param);
        else
            err = MyClassicEditTextFrameControlDefProc (
                                                    varCode,
                                                    control,
                                                    message,
                                                    param);
    }

    return err;
}

© 1999 Apple Computer, Inc. – (Last Updated 29 April 99)